package com.ustc.emailUI;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.Box;
import javax.swing.JFrame;

import com.ustc.emailcheck.Account;
import com.ustc.emailcheck.Monitor;

public class ConfigureInfoUI extends JFrame implements ActionListener {

	private JFrame jframe, sureMessageJframe, exceptionConfigureJframe;
	private Box boxV1, boxV2, boxV3, baseBox, baseBoxBottom, boxH1, boxH, boxV,boxV4;
	private Button submitButton, cancelButton, browseButton, sureButton, nsureButton,viewManagementButton,exitButton;
	private Choice serverTypeChoice;
	private FileDialog filedialog;
	private Container contentpane;
	private TextField nikName, server, accountName, password, viewTool;
	private Account account;
	

	public ConfigureInfoUI() {
		// TODO Auto-generated constructor stub
	}

//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//		new ConfigureInfoUI().showConfigureUI();
//
//	}

	public  void showConfigureUI() {
		/* jframe */
		jframe = new JFrame("configure info");
		jframe.setBounds(300, 350, 100, 100);
		jframe.setBackground(Color.gray);
		jframe.setVisible(true);
		jframe.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				jframe.setVisible(false);
			}
		});
		/* layout */
		baseBox = Box.createHorizontalBox();
		baseBoxBottom = Box.createHorizontalBox();
		boxV1 = Box.createVerticalBox();
		boxV2 = Box.createVerticalBox();
		boxV3 = Box.createVerticalBox();
		boxH1 = Box.createHorizontalBox();
		/* choice */
		serverTypeChoice = new Choice();
		serverTypeChoice.add("pop3");
		serverTypeChoice.add("imap");

		/* colume one */
		boxV1.add(new Label("Name:"));
		boxV1.add(Box.createVerticalStrut(8));
		boxV1.add(new Label("Incoming Server Type:"));
		boxV1.add(Box.createVerticalStrut(8));
		boxV1.add(new Label("Incoming Sever:"));
		boxV1.add(Box.createVerticalStrut(8));
		boxV1.add(new Label("Account Name:"));
		boxV1.add(Box.createVerticalStrut(8));
		boxV1.add(new Label("Password:"));
		boxV1.add(Box.createVerticalStrut(8));
		boxV1.add(new Label("View Email Client Tool:"));
		boxV1.add(Box.createVerticalStrut(8));
		/* colume two */
		boxV2.add(nikName = new TextField(16));
		boxV2.add(Box.createVerticalStrut(8));
		boxV2.add(serverTypeChoice);
		boxV2.add(Box.createVerticalStrut(8));
		boxV2.add(server = new TextField(16));
		boxV2.add(Box.createVerticalStrut(8));
		boxV2.add(accountName = new TextField(16));
		boxV2.add(Box.createVerticalStrut(8));
		boxV2.add(password = new TextField(16));
		password.setEchoChar('*');
		boxV2.add(Box.createVerticalStrut(8));
		boxV2.add(boxH1);
		boxV2.add(Box.createVerticalStrut(8));

		browseButton = new Button("browse");
		browseButton.addActionListener(this);
		boxH1.add(viewTool = new TextField(16));
		boxH1.add(browseButton);

		/* filedialog */
		filedialog = new FileDialog(jframe, "Choose View Email Client Tool",
				FileDialog.LOAD);
		filedialog.setVisible(false);
		filedialog.setAlwaysOnTop(true);
		filedialog.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				filedialog.setVisible(false);
			}
		});

		/* colume three */
		boxV3.add(new Label(" "));
		boxV3.add(Box.createVerticalStrut(8));
		boxV3.add(new Label("*"));
		boxV3.add(Box.createVerticalStrut(8));
		boxV3.add(new Label(
						"* you can get it from your email provider's website (eg. pop.126.com)"));
		boxV3.add(Box.createVerticalStrut(8));
		boxV3.add(new Label("* eg:someone@microsoft.com"));
		boxV3.add(Box.createVerticalStrut(8));
		boxV3.add(new Label("*"));
		boxV3.add(Box.createVerticalStrut(8));
		boxV3.add(new Label("* the directory of .exe file of IE,Firefox,Outlook,Thunderbird"));
		boxV3.add(Box.createVerticalStrut(8));
	
		baseBox.add(boxV1);
		baseBox.add(boxV2);
		baseBox.add(boxV3);

		submitButton = new Button("submit");
		submitButton.addActionListener(this);
		submitButton.setPreferredSize(new Dimension(80,20));
		submitButton.setMaximumSize(new Dimension(80,20));
		submitButton.setMinimumSize(new Dimension(80,20));
		cancelButton = new Button("cancel");
		cancelButton.addActionListener(this);
		cancelButton.setPreferredSize(new Dimension(80,20));
		cancelButton.setMaximumSize(new Dimension(80,20));
		cancelButton.setMinimumSize(new Dimension(80,20));
		
		baseBoxBottom.add(Box.createHorizontalStrut(100));
		baseBoxBottom.add(submitButton);
		baseBoxBottom.add(Box.createHorizontalStrut(100));
		baseBoxBottom.add(cancelButton);
		
		contentpane = jframe.getContentPane();
		contentpane.add(baseBox);
		contentpane.add(baseBoxBottom, BorderLayout.SOUTH);

		jframe.pack();

	}

	public void showMainPageUI() {
		sureMessageJframe = new JFrame("Confirm Message");
		sureMessageJframe.setBounds(400, 450, 100, 100);
		sureMessageJframe.setBackground(Color.gray);
		sureMessageJframe.setVisible(true);
		sureMessageJframe.setAlwaysOnTop(true);
		sureMessageJframe.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				sureMessageJframe.setVisible(false);
			}
		});
		
		boxV4=Box.createVerticalBox();
		boxV4.add(Box.createVerticalStrut(10));
		boxV4.add(new Label("Welcome to use Email Monitor!"));
		boxV4.add(new Label("Continue to create another account or manage your accounts?"));
		boxV4.add(Box.createHorizontalStrut(350));
		boxV4.add(Box.createVerticalStrut(10));

		boxH = Box.createHorizontalBox();
		sureButton = new Button("sure");
		sureButton.addActionListener(this);
		sureButton.setPreferredSize(new Dimension(80,20));
		sureButton.setMaximumSize(new Dimension(80,20));
		sureButton.setMinimumSize(new Dimension(80,20));
		nsureButton = new Button("cancel");
		nsureButton.addActionListener(this);
		nsureButton.setPreferredSize(new Dimension(80,20));
		nsureButton.setMaximumSize(new Dimension(80,20));
		nsureButton.setMinimumSize(new Dimension(80,20));
		viewManagementButton=new Button("manage account");
		viewManagementButton.addActionListener(this);
		viewManagementButton.setPreferredSize(new Dimension(150,20));
		viewManagementButton.setMaximumSize(new Dimension(150,20));
		viewManagementButton.setMinimumSize(new Dimension(150,20));
		exitButton=new Button("exit system");
		exitButton.addActionListener(this);
		exitButton.setPreferredSize(new Dimension(80,20));
		exitButton.setMaximumSize(new Dimension(80,20));
		exitButton.setMinimumSize(new Dimension(80,20));
		boxH.add(Box.createHorizontalStrut(5));
		boxH.add(sureButton);
		boxH.add(Box.createHorizontalStrut(5));
		boxH.add(nsureButton);
		boxH.add(Box.createHorizontalStrut(5));
		boxH.add(viewManagementButton);
		boxH.add(Box.createHorizontalStrut(5));
		boxH.add(exitButton);
		boxH.add(Box.createHorizontalStrut(5));

		contentpane = sureMessageJframe.getContentPane();
		contentpane.add(boxV4,BorderLayout.CENTER);
		contentpane.add(boxH, BorderLayout.SOUTH);
		sureMessageJframe.pack();

	}

	public void showExceptionConfigureUI() {

		exceptionConfigureJframe = new JFrame("warning");
		exceptionConfigureJframe.setVisible(true);
		exceptionConfigureJframe.setBackground(Color.red);
		exceptionConfigureJframe.setAlwaysOnTop(true);
		exceptionConfigureJframe.setLocation(400, 450);
		exceptionConfigureJframe.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				exceptionConfigureJframe.setVisible(false);
			}
		});

		boxV = Box.createVerticalBox();
		boxV.add(Box.createVerticalStrut(10));
		boxV.add(new Label("Create new account failed !"),
				BorderLayout.CENTER);
		boxV.add(Box.createHorizontalStrut(250));
		boxV.add(new Label("Please fill up all the fields marked by *"),
				BorderLayout.CENTER);
		boxV.add(Box.createVerticalStrut(10));

		contentpane = exceptionConfigureJframe.getContentPane();
		contentpane.add(boxV);
		exceptionConfigureJframe.pack();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == browseButton) {
			filedialog.setVisible(true);
			viewTool.setText(filedialog.getDirectory() + filedialog.getFile());
		}
		if (e.getSource() == submitButton) {
			
			if ("".equals(server.getText()) || "".equals(accountName.getText())
					|| "".equals(password.getText())
					|| "".equals(viewTool.getText())) {

				showExceptionConfigureUI();

			} 
			else {
				
				account=new Account();
				account.setName(nikName.getText());
				account.setServerType(serverTypeChoice.getSelectedItem());
				account.setServer(server.getText());
				account.setUsername(accountName.getText());
				account.setPassword(password.getText());
				account.setEmailclient(viewTool.getText());

				File accountfile=new File(Account.ACCOUNT_FILE);
				if(accountfile.exists()){
					Account[] exitAccounts=Account.loadAccount();
					int j=exitAccounts.length;
					Account[] accounts=new Account[j+1];
	
					for(int i=0;i<j;i++){
						accounts[i]=exitAccounts[i];
						}
					
					   accounts[j]=account;
					   Account.saveAccount(accounts);
					   jframe.setVisible(false);
					   showMainPageUI();
				
						
						
					}
				else{ 
					Account[] accounts=new Account[1];
					    accounts[0]=account;
					    Account.saveAccount(accounts);
					    jframe.setVisible(false);
					    
					}
				
				}
				
				
			}
			
		
		if (e.getSource() == cancelButton) {
			jframe.setVisible(false);
		}
		if (e.getSource() == sureButton) {
			sureMessageJframe.setVisible(false);
			showConfigureUI();

		}
		if (e.getSource() == nsureButton) {
			sureMessageJframe.setVisible(false);
		} 
		if(e.getSource()==viewManagementButton){
			sureMessageJframe.setVisible(false);
			new ManagementAccountUI().showManageAccountUI();
		}
		if(e.getSource()==exitButton){
			System.exit(0);
		}
		else {

		}
	}
	
	public Choice getServerTypeChoice() {
		return serverTypeChoice;
	}

	public void setServerTypeChoice(Choice serverTypeChoice) {
		this.serverTypeChoice = serverTypeChoice;
	}

	public TextField getNikName() {
		return nikName; 
	}

	public void setName(TextField name) {
		this.nikName = name;
	}

	public TextField getServer() {
		return server;
	}

	public void setServer(TextField server) {
		this.server = server;
	}

	public TextField getAccountName() {
		return accountName;
	}

	public void setAccountName(TextField accountName) {
		this.accountName = accountName;
	}

	public TextField getPassword() {
		return password;
	}

	public void setPassword(TextField password) {
		this.password = password;
	}

	public TextField getViewTool() {
		return viewTool;
	}

	public void setViewTool(TextField viewTool) {
		this.viewTool = viewTool;
	}

}
